package com.intelligent.ispc.web.controller;

import com.intelligent.ispc.common.dto.JsonPageResponseDto;
import com.intelligent.ispc.common.dto.JsonResponseDto;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.web.Servlets;
import com.intelligent.ispc.core.dto.LogDto;
import com.intelligent.ispc.core.service.LogService;
import com.intelligent.ispc.utils.SystemConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Peter on 15/11/23.
 */
@Controller
@RequestMapping(value = "/system")
public class LogController {

    @Autowired
    private SystemConfig systemConfig;

    @Autowired
    private LogService logService;

    @RequestMapping(value = "log", method = RequestMethod.GET)
    public String log(Model model, ServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isPermitted("system:log:view")) {
            return "error/noPermission";
        }
        return "system/log";
    }


    @RequestMapping(value = "log/detail/{id}", method = RequestMethod.GET)
    public String logDetail(@PathVariable("id") String id, Model model, ServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isPermitted("system:log:view")) {
            return "error/noPermission";
        }
        return "system/logDetail";
    }


    @ResponseBody
    @RequestMapping(value = "log/list")
    public JsonPageResponseDto list(Model model, ServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isPermitted("system:log:view")) {
            return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }
        Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");

        Integer start = 0;
        if (StringUtils.isNotBlank(request.getParameter("start"))) {
            start = Integer.valueOf(request.getParameter("start"));
        }
        Integer size = systemConfig.getPageSize();
        if (StringUtils.isNotBlank(request.getParameter("length"))) {
            size = Integer.valueOf(request.getParameter("length"));
        }

        Integer draw = 0;
        if (StringUtils.isNotBlank(request.getParameter("draw"))) {
            draw = Integer.valueOf(request.getParameter("draw"));
        }

        Integer pageNum = 0;
        if ((start / size) > 0) {
            pageNum = start / size;
        }

        Sort sort = null;
        String sortField = request.getParameter("sortField");
        if (StringUtils.isNotBlank(sortField)) {
            sort = new Sort(Sort.Direction.DESC, sortField);
        }

        Page<LogDto> page = logService.searchLog(searchParam, new PageRequest(pageNum, size, sort));
        List<LogDto> logs = page.getContent();

        JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(logs);
        json.setRecordsTotal(page.getTotalElements());
        json.setRecordsFiltered(page.getTotalElements());
        json.setDraw(draw);

        return json;
    }
}
